﻿#region

using System;
using System.Net;
using System.Windows;
using HudsonMonitor.Properties;

#endregion

namespace HudsonMonitor
{
    /// <summary>
    ///   Simple window to display settings for the application
    /// </summary>
    public partial class SettingsWindow
    {
        /// <summary>
        ///   Default constructor
        /// </summary>
        public SettingsWindow()
        {
            InitializeComponent();

            urlTextBox.Text = Settings.Default.HudsonUrl.Trim();
            TextBoxInterval.Text = Settings.Default.PollInterval.ToString();
            checkBoxRunAtStartup.IsChecked = Settings.Default.RunAtStartup;
        }

        /// <summary>
        ///   Event handler for clicking ok button
        /// </summary>
        /// <param name = "sender">Ok button</param>
        /// <param name = "e">Event details</param>
        private void OnOkClick(object sender, RoutedEventArgs e)
        {
            Settings.Default.HudsonUrl = urlTextBox.Text;
            Settings.Default.PollInterval = Convert.ToDouble(TextBoxInterval.Text);
            if (checkBoxRunAtStartup.IsChecked != null)
                Settings.Default.RunAtStartup = (bool)checkBoxRunAtStartup.IsChecked;
            Settings.Default.Save();
            DialogResult = true;
        }

        /// <summary>
        ///   Event handler for clicking cancel button
        /// </summary>
        /// <param name = "sender">Cancel button</param>
        /// <param name = "e">Event details</param>
        private void OnCancelClick(object sender, RoutedEventArgs e)
        {
            DialogResult = false;
        }

        private void OnUrlLostFocus(object sender, System.Windows.Input.KeyboardFocusChangedEventArgs e)
        {
            try
            {
                WebRequest.Create(urlTextBox.Text);
            }
            catch (UriFormatException)
            {
                MessageBox.Show("Url was not in a correct format", "Malformed Url", MessageBoxButton.OK);
            }
        }

        private void OnPollLostFocus(object sender, System.Windows.Input.KeyboardFocusChangedEventArgs e)
        {
            try
            {
                Convert.ToUInt16(TextBoxInterval.Text);
            }
            catch (FormatException)
            {
                MessageBox.Show("You must enter a positive number", "Poll interval error", MessageBoxButton.OK);
            }
        }

        private static void MakeProgramRunAtStartup(bool? run)
        {
            Microsoft.Win32.RegistryKey key = Microsoft.Win32.Registry.CurrentUser.OpenSubKey("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run", true);
            if (key != null)
            {
                if (run != null && run == true)
                    key.SetValue("Hudson Monitor", System.Reflection.Assembly.GetExecutingAssembly().Location);
                else
                    key.DeleteValue("Hudson Monitor", false);

                if (run != null) 
                    Settings.Default.RunAtStartup = (bool)run;
            }
        }

        private void OnRunAtStartupChecked(object sender, RoutedEventArgs e)
        {
            MakeProgramRunAtStartup(checkBoxRunAtStartup.IsChecked);
        }
    }
}